package simple.word;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;

public class DictMain {

	static void usage()
	{
		System.out.println("java -cp <classpath> " + DictMain.class.getName() + " filename");
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		if(args.length != 1)
		{
			usage();
			System.exit(1);
		}
		
		String fileName = args[0];
		File file = new File(fileName);
		if(!file.exists()) {
			System.out.println("Missing file : " + file.getAbsolutePath());
			System.exit(1);
		}
		
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String line;
		HashSet<String> lookuped = new HashSet<>();
		
		Lookup lookup = new Lookup();
		while((line = br.readLine()) != null) {
			String word = line.trim();
			try {
				LookupResult result = lookup.lookup(word);
				if (result != null && result.getExplain() != null
						&& result.getExplain().length() > 0) {
					
					if(lookuped.contains(result.getWord()))
						continue;
					//lookuped.add(word);

//					if(lookuped.contains(result.getWord()))
//						continue;
					
					lookuped.add(result.getWord());
					
					result.printWord(System.out);
					result.printExplain(System.out);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		
		br.close();
		fr.close();
		

	}

}
